/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EditTransactionPurchase.java
 *
 * Created on Nov 8, 2011, 3:52:35 AM
 */
package mpinventory;

import java.beans.PropertyVetoException;
import java.sql.*;
import java.util.List;
import java.util.ArrayList;
import com.michaelbaranov.microba.calendar.DatePicker;
import java.util.Calendar;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.DefaultListModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
/**
 *
 * @author msi
 */
public class EditTransactionPurchase extends javax.swing.JPanel {

    private Connection conn;
    private Dimension size;
    private DateFormat dateFormat=new SimpleDateFormat("MMMM dd, yyyy");
    private Calendar calendar=Calendar.getInstance();
    private Date date=new Date(calendar.get(calendar.YEAR)-1900,calendar.get(calendar.MONTH),calendar.get(calendar.DAY_OF_MONTH));
    private DatePicker datePicker=new DatePicker(date,dateFormat);
    public TransactionPurchaseQuery purchaseQuery;
    private ItemQuery itemQuery;
    private List<Item> items;
    public int width;
    public int height;
    public int screenWidth;
    public int screenHeight;
    private int selectedIndex;
    private int nRows=0;
    private int supplierID;
    public boolean backToMainMenu=false;
    private DefaultTableModel model;
    private String data[][] = {};
    private String col[] = {"Item", "Quantity", "Unit", "Price"};

    private List<Integer> origIDs;
    private List<Float> quantities;
    private List<String> units;
    private List<Integer> itemIDs;
    private List<String> descriptions;
    private List<Float> prices;
    /** Creates new form EditTransactionPurchase */
    public EditTransactionPurchase(Connection conn, int screenWidth, int screenHeight, int purchaseID, int supplierID, String supplierName, Date purchaseDate, String purchaseStatus, List<Integer> IDs, List<Float> quantities, List<String> units, List<Integer> itemIDs, List<String> itemNames, List<Float> prices) throws PropertyVetoException {
        this.conn=conn;
        this.screenWidth=screenWidth;
        this.screenHeight=screenHeight;
        myInitComponents();
        size=this.getPreferredSize();
        width=size.width+35;
        height=size.height+35;
        purchaseQuery=new TransactionPurchaseQuery(conn);
        txtTPEOrderID.setText(purchaseID+"");
        this.supplierID=supplierID;
        txtTPESupplier.setText(supplierName);
        datePicker.setDate(purchaseDate);
        date=purchaseDate;
        cbxTPEStatus.setSelectedItem(purchaseStatus);
        origIDs=IDs;
        this.itemIDs=itemIDs;
        this.quantities=quantities;
        this.units=units;
        this.descriptions=itemNames;
        this.prices=prices;
        model = new DefaultTableModel(data, col){
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Float.class, java.lang.String.class, java.lang.Float.class
            };
            boolean[] canEdit = new boolean [] {
                false, true, false, true
            };
            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        };
        tableTPECompanyOrders.setModel(model);
        tableTPECompanyOrders.getColumnModel().getColumn(0).setResizable(false);
        tableTPECompanyOrders.getColumnModel().getColumn(0).setPreferredWidth(250);
        tableTPECompanyOrders.getColumnModel().getColumn(1).setResizable(false);
        tableTPECompanyOrders.getColumnModel().getColumn(1).setPreferredWidth(10);
        tableTPECompanyOrders.getColumnModel().getColumn(2).setResizable(false);
        tableTPECompanyOrders.getColumnModel().getColumn(2).setPreferredWidth(10);
        tableTPECompanyOrders.getColumnModel().getColumn(3).setResizable(false);
        tableTPECompanyOrders.getColumnModel().getColumn(3).setPreferredWidth(60);
        initializeItemList();
        try{
        for(int i=0; i<itemIDs.size();i++){
            Object[] rowData=new Object[4];
            rowData[0]=itemNames.get(i);
            rowData[1]=quantities.get(i);
            rowData[2]=units.get(i);
            rowData[3]=prices.get(i);
            model.insertRow(nRows,rowData);
            nRows++;
        }
        }catch(Exception e){}
        tableTPECompanyOrders.setModel(model);
    }
    @Override
    public int getWidth(){
        return width;
    }
    @Override
    public int getHeight(){
        return height;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txtTPESupplier = new javax.swing.JTextField();
        txtTPEOrderID = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        cbxTPEStatus = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        listTPEItems = new javax.swing.JList();
        jLabel10 = new javax.swing.JLabel();
        btnTPEAdd = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();
        btnTPERemove = new javax.swing.JButton();
        btnTPESave = new javax.swing.JButton();
        btnTPECancel = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        tableTPECompanyOrders = new javax.swing.JTable();
        jLabel8 = new javax.swing.JLabel();

        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 25));
        jLabel1.setText("Edit Company Purchase");
        add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 11, -1, -1));

        jLabel3.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel3.setText("Purchase ID:");
        add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 70, -1, -1));

        jLabel5.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel5.setText("Supplier:");
        add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 110, -1, -1));

        jLabel6.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel6.setText("Date:");
        add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(450, 70, -1, -1));

        txtTPESupplier.setEditable(false);
        txtTPESupplier.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15));
        add(txtTPESupplier, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 110, 280, -1));

        txtTPEOrderID.setEditable(false);
        txtTPEOrderID.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15));
        add(txtTPEOrderID, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 70, 117, -1));

        jLabel4.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel4.setText("Status:");
        add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(450, 110, -1, -1));

        cbxTPEStatus.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15));
        cbxTPEStatus.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Pending", "Delivered" }));
        add(cbxTPEStatus, new org.netbeans.lib.awtextra.AbsoluteConstraints(530, 110, 175, -1));

        jLabel7.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel7.setText("List of all items:");
        add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 160, -1, -1));

        jScrollPane1.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15));

        listTPEItems.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15));
        listTPEItems.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Copper Tube S.D. 1/4\" x .022mm x 50'", "Copper Tube S.D. 3/8\" x .028mm x 50'", "Copper Tube S.D. 1/2\" x .028mm x 50'", "Copper Tube S.D. 5/8\" x .028mm x 50'", "Copper Tube S.D. 3/4\" x .028mm x 50'" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(listTPEItems);

        add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 200, 260, 146));

        jLabel10.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel10.setText("Add to -------------->");
        add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(290, 200, -1, -1));

        btnTPEAdd.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        btnTPEAdd.setText("Add");
        btnTPEAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addItemActionPerformed(evt);
            }
        });
        add(btnTPEAdd, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 240, 96, -1));

        jLabel9.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel9.setText("Remove from ----->");
        add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(290, 280, -1, -1));

        btnTPERemove.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        btnTPERemove.setText("Remove");
        btnTPERemove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeItemActionPerformed(evt);
            }
        });
        add(btnTPERemove, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 320, -1, -1));

        btnTPESave.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        btnTPESave.setText("Save");
        btnTPESave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveActionPerformed(evt);
            }
        });
        add(btnTPESave, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 360, 96, -1));

        btnTPECancel.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        btnTPECancel.setText("Cancel");
        btnTPECancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelActionPerformed(evt);
            }
        });
        add(btnTPECancel, new org.netbeans.lib.awtextra.AbsoluteConstraints(540, 360, 96, -1));

        tableTPECompanyOrders.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15)); // NOI18N
        tableTPECompanyOrders.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Item", "Quantity", "Unit", "Price"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Float.class, java.lang.String.class, java.lang.Float.class
            };
            boolean[] canEdit = new boolean [] {
                false, true, false, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tableTPECompanyOrders.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane2.setViewportView(tableTPECompanyOrders);
        tableTPECompanyOrders.getColumnModel().getColumn(0).setResizable(false);
        tableTPECompanyOrders.getColumnModel().getColumn(0).setPreferredWidth(250);
        tableTPECompanyOrders.getColumnModel().getColumn(1).setResizable(false);
        tableTPECompanyOrders.getColumnModel().getColumn(1).setPreferredWidth(10);
        tableTPECompanyOrders.getColumnModel().getColumn(2).setResizable(false);
        tableTPECompanyOrders.getColumnModel().getColumn(2).setPreferredWidth(10);
        tableTPECompanyOrders.getColumnModel().getColumn(3).setResizable(false);
        tableTPECompanyOrders.getColumnModel().getColumn(3).setPreferredWidth(60);

        add(jScrollPane2, new org.netbeans.lib.awtextra.AbsoluteConstraints(450, 200, 520, 146));

        jLabel8.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel8.setText("Company Orders:");
        add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(450, 160, -1, -1));
    }// </editor-fold>//GEN-END:initComponents
private void myInitComponents(){
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txtTPESupplier = new javax.swing.JTextField();
        txtTPEOrderID = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        cbxTPEStatus = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        listTPEItems = new javax.swing.JList();
        jLabel10 = new javax.swing.JLabel();
        btnTPEAdd = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();
        btnTPERemove = new javax.swing.JButton();
        btnTPESave = new javax.swing.JButton();
        btnTPECancel = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        tableTPECompanyOrders = new javax.swing.JTable();
        jLabel8 = new javax.swing.JLabel();

        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 25));
        jLabel1.setText("Edit Company Purchase");
        add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 11, -1, -1));

        jLabel3.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel3.setText("Purchase ID:");
        add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 70, -1, -1));

        jLabel5.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel5.setText("Supplier:");
        add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 110, -1, -1));

        jLabel6.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel6.setText("Date:");
        add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(450, 70, -1, -1));

        txtTPESupplier.setEditable(false);
        txtTPESupplier.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15));
        add(txtTPESupplier, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 110, 280, -1));

        txtTPEOrderID.setEditable(false);
        txtTPEOrderID.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15));
        add(txtTPEOrderID, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 70, 117, -1));

        jLabel4.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel4.setText("Status:");
        add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(450, 110, -1, -1));

        cbxTPEStatus.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15));
        cbxTPEStatus.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Pending", "Delivered" }));
        add(cbxTPEStatus, new org.netbeans.lib.awtextra.AbsoluteConstraints(530, 110, 175, -1));

        jLabel7.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel7.setText("List of all items:");
        add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 160, -1, -1));

        jScrollPane1.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15));

        listTPEItems.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15));
        listTPEItems.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Copper Tube S.D. 1/4\" x .022mm x 50'", "Copper Tube S.D. 3/8\" x .028mm x 50'", "Copper Tube S.D. 1/2\" x .028mm x 50'", "Copper Tube S.D. 5/8\" x .028mm x 50'", "Copper Tube S.D. 3/4\" x .028mm x 50'" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(listTPEItems);

        add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 200, 260, 146));

        jLabel10.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel10.setText("Add to -------------->");
        add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(290, 200, -1, -1));

        btnTPEAdd.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        btnTPEAdd.setText("Add");
        btnTPEAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addItemActionPerformed(evt);
            }
        });
        add(btnTPEAdd, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 240, 96, -1));

        jLabel9.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel9.setText("Remove from ----->");
        add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(290, 280, -1, -1));

        btnTPERemove.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        btnTPERemove.setText("Remove");
        btnTPERemove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeItemActionPerformed(evt);
            }
        });
        add(btnTPERemove, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 320, -1, -1));

        btnTPESave.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        btnTPESave.setText("Save");
        btnTPESave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveActionPerformed(evt);
            }
        });
        add(btnTPESave, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 360, 96, -1));

        btnTPECancel.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        btnTPECancel.setText("Cancel");
        btnTPECancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelActionPerformed(evt);
            }
        });
        add(btnTPECancel, new org.netbeans.lib.awtextra.AbsoluteConstraints(540, 360, 96, -1));

        tableTPECompanyOrders.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15)); // NOI18N
        tableTPECompanyOrders.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Item", "Quantity", "Unit", "Price"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Float.class, java.lang.String.class, java.lang.Float.class
            };
            boolean[] canEdit = new boolean [] {
                false, true, false, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tableTPECompanyOrders.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane2.setViewportView(tableTPECompanyOrders);
        tableTPECompanyOrders.getColumnModel().getColumn(0).setResizable(false);
        tableTPECompanyOrders.getColumnModel().getColumn(0).setPreferredWidth(250);
        tableTPECompanyOrders.getColumnModel().getColumn(1).setResizable(false);
        tableTPECompanyOrders.getColumnModel().getColumn(1).setPreferredWidth(10);
        tableTPECompanyOrders.getColumnModel().getColumn(2).setResizable(false);
        tableTPECompanyOrders.getColumnModel().getColumn(2).setPreferredWidth(10);
        tableTPECompanyOrders.getColumnModel().getColumn(3).setResizable(false);
        tableTPECompanyOrders.getColumnModel().getColumn(3).setPreferredWidth(60);

        add(jScrollPane2, new org.netbeans.lib.awtextra.AbsoluteConstraints(450, 200, 520, 146));

        jLabel8.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel8.setText("Company Orders:");
        add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(450, 160, -1, -1));

        datePicker.setShowNumberOfWeek(true);
        datePicker.setShowNoneButton(false);
        datePicker.setFieldEditable(false);
        add(datePicker, new org.netbeans.lib.awtextra.AbsoluteConstraints(530, 70, -1, -1));
}
    private void initializeItemList(){
        itemQuery=new ItemQuery(conn);
        items=itemQuery.initializeItems();
        DefaultListModel listModel = new DefaultListModel();
        for(int i=0;i<items.size();i++)
            listModel.addElement(items.get(i).getDescription());
        listTPEItems.setModel(listModel);
        selectedIndex=0;
        listTPEItems.addListSelectionListener(new ListSelectionListener(){
            @Override
            public void valueChanged(ListSelectionEvent e){
                try{
                    selectedIndex=listTPEItems.getSelectedIndex();
                }catch(Exception f){}
            }
        });
    } //end of initializeItemList() method
    private boolean validTable(){
        for(int i=0;i<model.getRowCount();i++)
            for(int j=0;j<model.getColumnCount();j++)
                try{
                    if(model.getValueAt(i,j).equals(null))
                        return false;
                }catch(NullPointerException e){
                    return false;
                }
        return true;
    } //end of validTable() method
    private boolean tableIsEmpty(){
        for(int i=0;i<model.getRowCount();i++)
            for(int j=0;j<model.getColumnCount();j++)
                try{
                    if(!model.getValueAt(i,j).toString().isEmpty())
                        return false;
                }catch(NullPointerException e){
                    return false;
                }
        return true;
    } //end of tableIsEmpty() method
    private void getTableValues(){
        descriptions=new ArrayList<String>();
        quantities=new ArrayList<Float>();
        String[] rowData=new String[model.getColumnCount()];
        for(int i=0;i<model.getRowCount();i++){
            for(int j=0;j<model.getColumnCount();j++)
                rowData[j]=model.getValueAt(i, j).toString();
            descriptions.add(rowData[0]);
            quantities.add(Float.parseFloat(rowData[1]));
            prices.add(Float.parseFloat(rowData[3]));
        }
    }
    private int getDayDiff(int year1, int month1, int day1, int year2, int month2, int day2){
        int dayDiff;
        int monthDiff;
        int yearDiff;
        if(day1<day2){
            day1+=30;
            month1-=1;
        }
        dayDiff=day1-day2;
        if(month1<month2){
            month1+=12;
            year1-=1;
        }
        monthDiff=month1-month2;
        yearDiff=year1-year2;
        dayDiff=yearDiff*12*30+monthDiff*30+dayDiff;
        return dayDiff;
    }
private void addItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addItemActionPerformed
        Object[] rowData=new Object[3];
        rowData[0]=items.get(selectedIndex).getDescription();
        rowData[2]=items.get(selectedIndex).getQuantityUnit();
        model.insertRow(nRows,rowData);
        nRows++;
        tableTPECompanyOrders.setModel(model);
        itemIDs.add(items.get(selectedIndex).getID());
}//GEN-LAST:event_addItemActionPerformed

private void removeItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeItemActionPerformed
        if(tableTPECompanyOrders.isEditing())
            JOptionPane.showMessageDialog(null, "Can't remove while editing a cell!");
        else if(tableTPECompanyOrders.getSelectedRow() == -1)
            JOptionPane.showMessageDialog(null, "Nothing selected!");
        else{
            itemIDs.remove(tableTPECompanyOrders.getSelectedRow());
            model.removeRow(tableTPECompanyOrders.getSelectedRow());
            nRows--;
        }
        tableTPECompanyOrders.setModel(model);
}//GEN-LAST:event_removeItemActionPerformed

private void saveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveActionPerformed
        purchaseQuery=new TransactionPurchaseQuery(conn);
        if(tableTPECompanyOrders.isEditing())
            JOptionPane.showMessageDialog(null, "Can't save while editing a cell!");
        else if(txtTPESupplier.getText().equals("") || !validTable() || tableIsEmpty())
            JOptionPane.showMessageDialog(null, "All fields cannot be empty!");
        else if(datePicker.getDate().after(date))
            JOptionPane.showMessageDialog(null, "Stay on the current time!");
        else if(getDayDiff(date.getYear(),date.getMonth(),date.getDate(),datePicker.getDate().getYear(),datePicker.getDate().getMonth(),datePicker.getDate().getDate())>180)
            JOptionPane.showMessageDialog(null, "Stay on the current time!");
        else{
            JOptionPane pane=new JOptionPane("Are you sure you want to edit this purchase?");
            Object[] options=new String[]{"Yes","No"};
            pane.setOptions(options);
            JDialog dialog = pane.createDialog(new JFrame(), "Confirm Purchase Edition");
            dialog.show();
            if(options[0].equals(pane.getValue())){
                getTableValues();
                purchaseQuery.editPurchase(Integer.parseInt(txtTPEOrderID.getText()),supplierID,txtTPESupplier.getText(),datePicker.getDate(),cbxTPEStatus.getSelectedItem().toString(),quantities,itemIDs,descriptions,prices,origIDs);
                backToMainMenu=true;
            } //YES
        }
}//GEN-LAST:event_saveActionPerformed

private void cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelActionPerformed
        JOptionPane pane=new JOptionPane("Are you sure you want to go back to the main menu? Please make sure that all changes have been saved.");
        Object[] options=new String[]{"Yes","No"};
        pane.setOptions(options);
        JDialog dialog = pane.createDialog(new JFrame(), "Back To Main Menu?");
        dialog.show();
        if(options[0].equals(pane.getValue())) //YES
            backToMainMenu=true;
}//GEN-LAST:event_cancelActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnTPEAdd;
    private javax.swing.JButton btnTPECancel;
    private javax.swing.JButton btnTPERemove;
    private javax.swing.JButton btnTPESave;
    private javax.swing.JComboBox cbxTPEStatus;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JList listTPEItems;
    private javax.swing.JTable tableTPECompanyOrders;
    private javax.swing.JTextField txtTPEOrderID;
    private javax.swing.JTextField txtTPESupplier;
    // End of variables declaration//GEN-END:variables
}
